package com.sjksy.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sjksy.demo.domain.Branch;
import com.sjksy.demo.domain.CustomerInformation;
import com.sjksy.demo.domain.Ledger;
import com.sjksy.demo.domain.TravelRoute;
import com.sjksy.demo.dto.guide.GuideInfoDto;
import com.sjksy.demo.dto.ledger.*;
import com.sjksy.demo.mapper.*;
import com.sjksy.demo.msmapper.ledger.LedgerMsMapper;
import com.sjksy.demo.service.LedgerService;
import com.sjksy.demo.vo.ledger.LedgerBranchStatisticVo;
import com.sjksy.demo.vo.ledger.LedgerGuideStatisticVo;
import com.sjksy.demo.vo.ledger.LedgerRouteStatisticVo;
import com.sjksy.demo.vo.ledger.LedgerVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author guyueyuan
* @description 针对表【ledger】的数据库操作Service实现
* @createDate 2024-12-11 15:37:02
*/
@Service
public class LedgerServiceImpl extends ServiceImpl<LedgerMapper, Ledger>
    implements LedgerService{

    @Resource
    LedgerMapper ledgerMapper;
    @Resource
    GuideMapper guideMapper;
    @Resource
    LedgerMsMapper ledgerMsMapper;
    @Resource
    TravelRouteMapper travelRouteMapper;
    @Resource
    CustomerInformationMapper customerInformationMapper;
    @Resource
    BranchMapper branchMapper;


    /**
    * 插入分账信息
    * @param ledgerInsertDto 分账信息的dto（可以是未插入分公司id的）
    * */
    @Override
    public boolean insertLedger(LedgerInsertDto ledgerInsertDto) {
        Integer guideId=ledgerInsertDto.getGuideId();
        Integer branchIdByGuideId = guideMapper.getBranchIdByGuideId(guideId);
        ledgerInsertDto.setBranchId(branchIdByGuideId);
        ledgerInsertDto.setTime(new Date());
        return ledgerMapper.insert(ledgerMsMapper.insertDtoToLedger(ledgerInsertDto))>0;
    }

//    网上普遍说小型数据使用大连表，小型数据使用分多次查
    /**
     * 获取所有分账信息，这里会不会有数据查询瓶颈
     * */
    @Override
    public List<LedgerVo> getAllLedger() {
        List<LedgerVo> ledgerVoList=new ArrayList<>();
        List<Ledger> ledgers = ledgerMapper.selectList(null);
        ledgers.forEach((ledger -> {
            TravelRoute travelRoute = travelRouteMapper.selectById(ledger.getRouteId());
            CustomerInformation customerInformation = customerInformationMapper.selectById(ledger.getCustomerId());
            Branch branch = branchMapper.selectById(ledger.getBranchId());
            GuideInfoDto guideInfoDto = guideMapper.getEmployeeInfoByGuideId(ledger.getGuideId());
            LedgerDto ledgerDto = ledgerMsMapper.ledgerToDto(ledger, travelRoute, customerInformation, branch,guideInfoDto);
            ledgerDto.setMoney(customerInformationMapper.getMoneyByCustomerId(ledgerDto.getCustomerId()));
            ledgerVoList.add(ledgerMsMapper.DtoLedgerToLedgerVo(ledgerDto));
        }));
        return ledgerVoList;
    }

    /**
     * 统计当月路线销售情况
     * */
    @Override
    public List<LedgerRouteStatisticVo> statisticRouteLedger() {
        List<LedgerRouteStatisticDto> ledgerRouteStatisticDtoList = ledgerMapper.statisticRouteLedger();
        return ledgerMsMapper.DtoLedgerRouteStatisticToVo(ledgerRouteStatisticDtoList);
    }

    /**
     * 统计当月导游营业情况
     * */
    @Override
    public List<LedgerGuideStatisticVo> statisticGuideLedger() {
        List<LedgerGuideStatisticDto> ledgerGuideStatisticDtoList = ledgerMapper.statisticGuideLedger();
        return ledgerMsMapper.DtoLedgerGuideStatisticToVo(ledgerGuideStatisticDtoList);
    }

    /**
     * 统计上月分公司营业情况
     * */
    @Override
    public List<LedgerBranchStatisticVo> statisticBranchLedger() {
        List<LedgerBranchStatisticDto> ledgerBranchStatisticDtoList = ledgerMapper.statisticBranchLedger();
        return ledgerMsMapper.DtoLedgerBranchStatisticToVo(ledgerBranchStatisticDtoList);
    }
}




